<template>
  <el-dialog width="800px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="模具名称" prop="moldName">
            <el-input v-model="submitForm.moldName" placeholder="请输入模具名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类" prop="categoryId">
            <category-query ref="categoryQuery" v-model="submitForm.categoryId" @choose="handleChooseCategory" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="unit">
            <dict-select v-model="submitForm.unit" dict-key="prod_unit" placeholder="请选择单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商" prop="supplierId">
            <supplier-query ref="supplierQuery" v-model="submitForm.supplierId" @choose="handleChooseSupplier" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造日期" prop="madeDate">
            <el-date-picker
              v-model="submitForm.madeDate"
              style="width: 100%"
              type="date"
              placeholder="请选择制造日期"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产能" prop="capacity">
            <el-input v-model="submitForm.capacity" type="number" placeholder="请输入产能 比如一模出三双则填3" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="重量" prop="weight">
            <el-input v-model="submitForm.weight" type="number" placeholder="请输入鞋底重量 单位为克" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="位置" prop="position">
            <el-input v-model="submitForm.position" placeholder="请输入货架编号 例如:1-3-10002" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="submitForm.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 2}" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveMold, updateMoldById, selectMoldById } from '@/api/erp/mold'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        id: '',
        moldName: '',
        categoryId: '',
        unit: '',
        supplierId: '',
        madeDate: '',
        capacity: '',
        weight: '',
        position: '',
        remark: ''
      },
      submitFormRules: {
        moldName: [{ required: true, message: '请输入模具名称', trigger: 'change' }],
        categoryId: [{ required: true, message: '请选择分类', trigger: 'change' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        // supplierId: [{ required: true, message: '请选择供应商', trigger: 'change' }],
        madeDate: [{ required: true, message: '请选择制造日期', trigger: 'change' }],
        capacity: [{ required: true, message: '请输入产能', trigger: 'change' }],
        weight: [{ required: true, message: '请输入重量', trigger: 'change' }],
        position: [{ required: true, message: '请输入模具位置', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增模具'
        return
      }
      this.dialogTitle = '修改模具'
      this.formLoading = true
      selectMoldById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.$refs.categoryQuery.init(this.submitForm.categoryId)
          this.$refs.supplierQuery.init(this.submitForm.supplierName)
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      console.log(this.submitForm)
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveMold
          if (this.submitForm.id) {
            request = updateMoldById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    },
    handleChooseCategory(categoryId) {
      if (categoryId) {
        this.submitForm.categoryId = categoryId
      }
    },
    handleChooseSupplier(row) {
      if (row) {
        this.submitForm.supplierId = row.id
      }
    }
  }
}
</script>

<style scoped>

</style>
